package jdk.jdk8.functioninterface.lambdaapi;

import jdk.jdk8.functioninterface.lambdaapi.entity.Mask;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 断定型接口，判断是否满足约束条件
 * Predicate接口一共有3个默认方法：negate、and和or，用它们可以创建更加复杂的Predicate接口实例。
 *
 * @author guodd
 * @version 1.0
 * @since 1.8
 */
public class PredicateTest {
    // negate方法就是做非运算
    @Test
    public void test() {
        Mask mask = new Mask("Honeywell", "N95", 19.5);
        Predicate<Mask> isN95 = m -> "N95".equals(m.getType());
        // 判断口罩类型是N95
        System.out.println(isN95.test(mask));
        // 判断口罩类型不是N95
        System.out.println(isN95.negate().test(mask));
    }

    @Test
    public void filter() {
        List<String> a = filterList(Arrays.asList("aa", "ab", "ac", "dd"), e -> e.contains("a"));
        System.out.println(a);
    }

    public List<String> filterList(List<String> strList, Predicate<String> supplier) {
        ArrayList<String> newStr = new ArrayList<>();
        for (String s : strList) {
            if (supplier.test(s)) {
                newStr.add(s);
            }
        }
        return newStr;
    }
}
